package cn.fancy.sdk.pay.ali;

import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;
import cn.fancy.sdk.common.utils.ResUtil;
import cn.fancy.sdk.common.utils.SdkAsyncTask;
import cn.fancy.sdk.entity.GuildBaseInfo;
import cn.fancy.sdk.login.utils.HttpReq;
import cn.fancy.sdk.pay.GuildPayActivity;
import cn.fancy.sdk.pay.utils.PayAdvertUpload;
import cn.fancy.sdk.pay.view.PayDetailView;
import cn.fancy.sdk.utils.TipMessSingle;
import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;

public class AlipayView extends PayDetailView implements OnClickListener {
    
    public static final int REQ_ALI = 10045;    //随意定义的resultCode
    public static final String REQ_EXTRA_MONEY = "req_extra_money";
    
    private long lastTime = 0;
    private String netTime;
    
    public AlipayView(GuildPayActivity activity, int id) {
        super(activity, id);
    }
    
    @Override
    public void activityCreated() {
        initView();
    }
    
    public void initView() {
        detailConfirmBt.setOnClickListener(this);
    }
    
    @Override
    public void onClick(View v) {
        long time = System.currentTimeMillis();
        
        if (time - lastTime < 1500) {
            return;
        } else {
            lastTime = time;
        }
        
        int vid = v.getId();
        if (vid == ResUtil.getId(activity, "guild_paydetail_confirm")) {
            Log.e("alipaybtn", "alipaybtn");
            // 聚合传的money是10元，传给后台时是用1000分
            final String payMoney = String.valueOf(
                (int)(activity.cppayment.getDiscountMoney(this.activity) * 100)
            );
            final String realMoney = String.valueOf((int)(activity.cppayment.getMoney() * 100));
            final String payorder = activity.cppayment.getExt();
            final String paydesc = activity.cppayment.getItem_desc();
            final String cpData = activity.cppayment.getCpData();
    
            final SdkAsyncTask<String> payAsyncTask = new SdkAsyncTask<String>() {
                @Override
                protected String doInBackground() {
                    final Map<String, String> map = GuildBaseInfo.getInstance().getPayParamsFancy(activity,
                        payMoney, realMoney, paydesc, payorder, cpData, String.valueOf(paymentId), netTime);
                    
                    Log.e("alipayparam", "alipayparam" + map.toString());
                    return HttpReq.doGuildRequest(map);
                }
    
                @Override
                protected void onPostExecute(String rst) {
                    Log.e("alipayrst", "alipayrst" + rst);
                    if (rst == null || rst.equals("")) {
                        Toast.makeText(activity, TipMessSingle.ten_networkerr, Toast.LENGTH_LONG).show();
                        return;
                    }
                    try {
                        JSONObject json = new JSONObject(rst);
                        String state = json.getString("state");
                        JSONObject jsonState = new JSONObject(state);
                        
                        int code = jsonState.getInt("code");
                        String msgmsg = jsonState.optString("msg");
                        if (code != 1) {
                            Toast.makeText(activity, msgmsg, Toast.LENGTH_LONG).show();
                            return;
                        }
                        String msg = json.getString("data");
                        JSONObject msgjson = new JSONObject(msg);
                        String url = msgjson.getString("pay_info");
                        
                        Log.e("alipayintent", "alipayintent" + url);
                        Intent intent = new Intent(activity, AlipayH5Activity.class);
                        intent.putExtra("url", url);
                        intent.putExtra("paytype", 2);
                        intent.putExtra("money", payMoney);
                        intent.putExtra("paymentId", paymentId);
                        activity.startActivityForResult(intent,REQ_ALI);
                        activity.finish();
    
                        PayAdvertUpload.uploadPayIfExistAdvert(activity, "alipay", paydesc, payorder,
                            (int)activity.cppayment.getMoney() * 100);
                    } catch (JSONException e) {
                        Log.d("BindPhone", "Server returns error msg");
                        e.printStackTrace();
                    }
                }
                
                @Override
                public Activity getOwnerActivity() {
                    return activity;
                }
                
                @Override
                protected void onCancelled() {
                }
            };
    
            SdkAsyncTask<String> netTimeAsyncTask = new SdkAsyncTask<String>() {
        
                @Override
                public Activity getOwnerActivity() {
                    return activity;
                }
        
                @Override
                protected String doInBackground() {
                    return GuildBaseInfo.getTime(activity);
                }
        
                @Override
                protected void onPostExecute(String time) {
                    netTime = time;
                    payAsyncTask.execute();
                }
        
                @Override
                protected void onCancelled() {
            
                }
            };
            netTimeAsyncTask.execute();
        } else if (vid == ResUtil.getId(activity, "guild_pay_chargemoney")) {
            selectAmount();
        }
    }
    
}
